/*****************************  ������֤  ****************************/

/**
 * ������֤
 * @param div
 * @returns {noCaptcha}
 */
function getNoCaptcha(div, width)
{
    var nc_scene = 'nc_login';  //����,���ɸ���
    var nc_appkey = 'FFFF0N0N00000000117B';  // Ӧ�ñ�ʶ,���ɸ���
    // var nc_appkey = 'code300';  // Ӧ�ñ�ʶ,���ɸ���
    var nc_token = [nc_appkey, (new Date()).getTime(), Math.random()].join(':');
    var NC_Opt =
        {
            renderTo: "#" + div,
            appkey: nc_appkey,
            scene: nc_scene,
            token: nc_token,
            customWidth: width,
            is_Opt: 0,
            language: "cn",
            isEnabled: true,
            timeout: 3000,
            times:5,
            callback: function (data) {
                $('#'+div+'-sig').val(data.sig);
                $('#'+div+'-token').val(nc_token);
                $('#'+div+'-scene').val(nc_scene);
                $('#'+div+'-csessionid').val(data.csessionid);
                $(".ph-info .password,.bind_code_p").removeClass("active");
            }
        };
    return new noCaptcha(NC_Opt);
}

/**
 * ������֤����
 * @param div
 */
function showCaptcha(div, width)
{
    var nc = getNoCaptcha(div, width);
    nc.upLang('cn', {
        _startTEXT: "�밴ס���飬�϶������ұ�",
        _yesTEXT: "��֤ͨ��",
        _error300: "��ѽ�������ˣ����<a href=\"javascript:__nc.reset()\">ˢ��</a>����һ��",
        _errorNetwork: "���粻��������<a href=\"javascript:__nc.reset()\">���ˢ��</a>",
    });
}

/**
 * ������֤����
 * @param div
 */
function resetCaptcha(div, width) {
    var nc = getNoCaptcha(div, width);
    nc.reload();
}


/*****************************  ����ҵ���߼�  ****************************/

// var show_num = [];

//��½��
function _login() {
    $(".login-father-level").css('display','block');
    $("#login_ways").css('display','block');
}

//��¼��
$(".login-pop-click").on('click',function(){
    showCaptcha('login-slide-div',300);
    _login();
});

$('.login-qq-i').click(function () {
	$(".icon-close").click();
    //window.location.href="//ibaotu.com/?m=login&a=snsLogin&type=qq&ss=1";
	QC.Login.showPopup({
        appId: "101515449",
        redirectURI: "http://5i0z4.cn/protal/qqNotify"
    });
});

QC.api("get_user_info", {}).success(function (s) {
    //成功回调，通过s.data获取OpenAPI的返回数据
    var nickname = s.data.nickname;
    var figureurl = s.data.figureurl_qq_1;
    if (QC.Login.check()) {
        QC.Login.getMe(function (openId, accessToken) {
            // 获取用户信息
        	$(".unlogined").hide();
        	$(".logined").show();
        	$(".userimg").find("img").attr("src",figureurl);
        	$(".userinfo").find("p").html(nickname);
        	$(".userimgx").find("img").attr("src",figureurl);
        	$(".userinfox").find("p").html(nickname);
        	isLogin = true;
        })
    }
}).error(function (f) {
    //失败回调
    alert("获取用户信息失败！");
});

$('.login-ww-i').click(function () {
    window.location.href="//ibaotu.com/?m=login&a=snsLogin&type=weixin&ss=1"
});

$('.sign-out').click(function () {
    // window.location.href="//ibaotu.com/?m=login&a=logouts"
    window.location.href="/user/signOut"
});

//�ֻ��ŵ�½
$(".mob-login-sub").on('click',function(){
    var _phone = $(".login_phone_input").val();
    var _code = $("#mo-veri").val();

    if(_phone.length != 11) {
        $(".ph-info .cell").addClass("active");
        return false
    }
    if(_code.length != 4) {
        $(".ph-info .password .login_Text").html('�ף�������4λ����֤��');
        $(".ph-info .password").addClass("active");
        return false
    }
    $.ajax({
        type:'post',
        data:{phone:_phone,code:_code},
        url:'/user/verifyCode',
        dataType:'json',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },

        success:function(rs){
            if(rs.status == 0){
                if(rs.type == 1){
                    $(".ph-info .cell .login_Text").html(rs.msg);
                    $(".ph-info .cell").addClass("active");
                }
                if(rs.type == 3){
                    $(".ph-info .password .login_Text").html(rs.msg);
                    $(".ph-info .password").addClass("active");
                }
            }
            if(rs.status == 1){
                location.reload();
            }
            if(rs.status == 2){
                $.each(rs.list, function(index, value) {
                    $('.account-bind-info-'+index+' img').attr('src',value.head);
                    $('.account-bind-info-'+index+' b').html(value.id);
                    $('.account-bind-info-'+index+' span').html(value.username);
                    $('.account-bind-info-'+index).attr('data-id',value.id);
                });

                $(".bd_login_x").show();
                $(".s_login_c").hide();
            }
        }
    });
});

//�������ֻ���
$('.choice-binding-phone').on('click',function(){
    var _id = $(this).attr("data-id");
    $.ajax({
        type:'post',
        data:{id:_id},
        url:'/user/untieSurplusPhone',
        dataType:'json',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success:function(rs){
            if(rs.status == 0){
                alert(rs.msg);
            }
            if(rs.status == 1){
                location.reload();
            }
        }
    });
});


// �ֻ���֤
$(".login_phone_input").on("blur",function () {
    var phone_len = $(".login_phone_input").val();
    if(phone_len.length<11){
        $(".ph-info .cell").addClass("active");
    }else{
        return;
    }
});

$(".login_phone_input").on("focus",function () {
    $(".ph-info .cell").removeClass("active");
});
$(".login_code_input").on("focus",function () {
    $(".ph-info .password").removeClass("active");
});

$(".login_code_input,.login_phone_input").on("input",function () {
    var phone_len = $(".login_phone_input").val();
    // var img_len = $(".login_img_input").val();
    var code_len = $(".login_code_input").val();
    if((phone_len.length == 11) && (code_len.length == 4)){
        $(".mob-login-sub").addClass("active");
    }
});


var countdown = 60;//������֤����ʱ��

//�����ֻ���֤��
$(".ver-btn").on('click', function(){
    if(countdown == 60){
        var _phone_len = $(".login_phone_input").val();
        var _sig = $("#login-slide-div-sig").val();
        var _token = $("#login-slide-div-token").val();
        var _scene = $("#login-slide-div-scene").val();
        var _csessionid = $("#login-slide-div-csessionid").val();
        if(_phone_len.length < 11){
            $(".ph-info .cell").addClass("active");
            return false;
        }
        if(!_csessionid || !_sig || !_token || !_scene){
            $(".ph-info .password .login_Text").html('�ף��뻬����֤��');
            $(".ph-info .password").addClass("active");
            return false;
        }

        $.ajax({
            type:'post',
            data:{phone:_phone_len,sig:_sig,token:_token,scene:_scene,csessionid:_csessionid},
            url:"/user/sendOutCodeeSlide",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            dataType:'json',
            success:function(rs){
                if(rs.status == 0){
                    resetCaptcha('login-slide-div',300);
                    $(".ph-info .password .login_Text").html(rs.msg);
                    $(".ph-info .password").addClass("active");
                }else{
                    sendemail();
                }
            }
        });

    }
});

function sendemail(){
    var obj = $(".ver-btn");
    settime(obj);
}

function settime(obj) { //������֤�뵹��ʱ
    if (countdown == 0) {
        obj.html("������֤��").css("color","#ff9100");
        countdown = 60;
        return;
    } else {
        obj.html(""+countdown+"s������·���").css("color","#999999");
        countdown--;
    }
    setTimeout(function() { settime(obj) },800);
}

//�ֻ��Ű󶨵���
$('.phone_bind_click').click(function () {
    showCaptcha('bind-slide-div',300);
    $(".phone_bind_pop").css('display','block');
});

// �ֻ���֤
$(".bind_phone_input").on("blur",function () {
    var phone_len = $(".bind_phone_input").val();
    if(phone_len.length<11){
        $(".bind_phone_p").addClass("active");
    }
});

$(".bind_phone_input").on("focus",function () {
    $(".bind_phone_p").removeClass("active");
});
$(".bind_code_input").on("focus",function () {
    $(".bind_code_p").removeClass("active");
});

$(".bind_phone_input,.bind_code_input").on("input",function () {
    var phone_len = $(".bind_phone_input").val();
    var code_len = $(".bind_code_input").val();
    if((phone_len.length == 11) && (code_len.length == 4)){
        $(".mobile_bind_sub").addClass("active");
    }
});


var countdown = 60;//������֤����ʱ��

//�����ֻ���֤��
$(".bind_code").on('click', function(){
    if(countdown == 60){
        var _phone_len = $(".bind_phone_input").val();

        var _sig = $("#bind-slide-div-sig").val();
        var _token = $("#bind-slide-div-token").val();
        var _scene = $("#bind-slide-div-scene").val();
        var _csessionid = $("#bind-slide-div-csessionid").val();

        if(_phone_len.length < 11){
            $(".bind_phone_p").addClass("active");
            return false;
        }
        if(!_csessionid || !_sig || !_token || !_scene){
            $(".bind_code_p").html('�ף��뻬����֤��');
            $(".bind_code_p").addClass("active");
            return false;
        }

        $.ajax({
            type:'post',
            data:{phone:_phone_len,sig:_sig,token:_token,scene:_scene,csessionid:_csessionid},
            url:"/user/sendOutCodeeSlide",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            dataType:'json',
            success:function(rs){
                if(rs.status == 0){
                    resetCaptcha('bind-slide-div',300);
                    $(".bind_code_p").html(rs.msg);
                    $(".bind_code_p").addClass("active");
                }else{
                    var obj = $(".bind_code");
                    settime(obj);
                }
            }
        });
    }
});

//�ֻ��Ű�
$(".mobile_bind_sub").on('click',function(){

    var _phone = $(".bind_phone_input").val();
    var _code = $(".bind_code_input").val();

    if(_phone.length != 11) {
        $(".bind_phone_p").addClass("active");
        return false
    }
    if(_code.length != 4) {
        $(".bind_code_p").html('�ף�������4λ����֤��');
        $(".bind_code_p").addClass("active");
        return false
    }

    $.ajax({
        type:'post',
        data:{phone:_phone,code:_code},
        url:'/user/mobileBind',
        dataType:'json',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },

        success:function(rs){
            if(rs.status == 0){
                if(rs.type == 1){
                    $(".bind_phone_p").html(rs.msg);
                    $(".bind_phone_p").addClass("active");
                }
                if(rs.type == 3){
                    $(".bind_code_p").html(rs.msg);
                    $(".bind_code_p").addClass("active");
                }
            }
            if(rs.status == 1){
                var _url = window.location.href;
                if(_url.indexOf("/user/info") >= 0) {
                    window.location.reload();
                }

                $(".phone_bind_pop").css('display','none');
            }
        }
    });
});

function settime(obj) { //������֤�뵹��ʱ
    if (countdown == 0) {
        obj.html("������֤��").css("color","#ff9100");
        countdown = 60;
        return;
    } else {
        obj.html(""+countdown+"s������·���").css("color","#999999");
        countdown--;
    }
    setTimeout(function() { settime(obj) },800);
}